package com.zjx.reggie.controller;



import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zjx.reggie.common.R;
import com.zjx.reggie.domain.ShoppingCart;
import com.zjx.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;


@Slf4j
@RequestMapping("/shoppingCart")
@RestController
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart, HttpSession session){
    log.info(shoppingCart.toString());
    //获取当前登录用户的id，为了确定该用户向购物车中添加了那些菜品和套餐
        Long userId = (Long) session.getAttribute("user");
        shoppingCart.setUserId(userId);
        //创建条件构造器，通过当前用户查询购物车中的数据；
        LambdaQueryWrapper<ShoppingCart> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,userId);

        //判断用户向购物车中添加的是菜品还是套餐；
        Long dishId = shoppingCart.getDishId();
        if (dishId !=null){
            //如果前端传过来的数据，菜品的id不为null，说明用户添加的是菜品而不是套餐；
            queryWrapper.eq(ShoppingCart::getDishId,dishId);
        }else{
            //就是用户添加的是套餐,向条件构造器中添加套餐id条件
            queryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        //通过上述判断，添加相应的条件，去数据库中查询唯一的数据；
        ShoppingCart shoppingCartServiceOne = shoppingCartService.getOne(queryWrapper);

        if (shoppingCartServiceOne !=null){
            Integer number = shoppingCartServiceOne.getNumber();
            shoppingCartServiceOne.setNumber(number+1);
            shoppingCartService.updateById(shoppingCartServiceOne);
        }else {
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);

            //懵逼
            shoppingCartServiceOne = shoppingCart;
        }
     return  R.success(shoppingCartServiceOne);
    }

    /**
     * 查看购物车功能
     * @param session
     * @return
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> list(HttpSession session){
    Long userId = (Long) session.getAttribute("user");
    LambdaQueryWrapper<ShoppingCart> queryWrapper=new LambdaQueryWrapper<>();
    queryWrapper.eq(ShoppingCart::getUserId,userId);
    List<ShoppingCart> shoppingCartList = shoppingCartService.list(queryWrapper);
    return R.success(shoppingCartList);
    }

    /**
     * 清空购物车功能
     * @param session
     * @return
     */
    @DeleteMapping("/clean")
    public R<String> clean(HttpSession session){
        Long userId = (Long) session.getAttribute("user");
        LambdaQueryWrapper<ShoppingCart> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ShoppingCart::getUserId,userId);
        shoppingCartService.remove(lambdaQueryWrapper);
        return R.success("购物车清空成功");
    }





}
